package com.zmn.mcc.admin.controller.staff.stafffamily;


import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.common.dictionary.StaffFamilyDict;
import com.zmn.mcc.model.entity.staff.McStaff;
import com.zmn.mcc.model.entity.staff.stafffamily.StaffFamily;
import com.zmn.mcc.model.entity.staff.stafffamily.StaffFamilyQuery;
import com.zmn.mcc.services.interfaces.staff.stafffamily.StaffFamilyService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 家庭关系和联系人基础查询Controller
 *
 * @author tanghongjun
 * @since 2020-05-14 11:15:18
 */
@RestController
@RequestMapping("/staff/stafffamily/")
public class StaffFamilyListController extends StaffFamilyBaseController {

    private Logger logger = LoggerFactory.getLogger(StaffFamilyListController.class);

    @Resource
    private StaffFamilyService staffFamilyService;

    /**
     * 列表
     */
    @RequestMapping("/listByStaffId")
//    @RequiresPermissions("staff/stafffamily/list.action")
    public ModelAndView listByStaffId(@RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {
        StaffFamilyQuery query = new StaffFamilyQuery();
        query.setStaffId(staffId);

        return this.list(query);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("staff/stafffamily/list.action")
    public ModelAndView list(@ModelAttribute StaffFamilyQuery query) {
        if (Objects.isNull(query.getStaffId())) {
            logger.error("员工ID不能为空" + query.getStaffId());
            return null;
        }

        McStaff obj = staffService.findByKey(query.getStaffId());

        List<StaffFamily> items = staffFamilyService.listPageByQuery(query);
        items.stream().forEach(e -> {
            e.setMobile(StringUtils.isNotEmpty(e.getMobile()) ? StringUtil.formatHideMobile(e.getMobile()) : e.getMobile());
        });

        ModelAndView mav = new ModelAndView("staff/stafffamily/list");
        mav.addObject("items", items);
        mav.addObject("vo", query);
        mav.addObject("obj", obj);
        mav.addObject("flag", 4);

        return mav;
    }


    /**
     * 信息
     */
    @RequestMapping("/addEditView")
//    @RequiresPermissions("staff/stafffamily/addEditView.action")
    public ModelAndView info(@RequestParam(value = "familyId", defaultValue = "0") Integer familyId,
                             @RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        StaffFamily staffFamily;
        if (familyId == 0) {
            staffFamily = new StaffFamily();
            staffFamily.setStaffId(staffId);
        } else {
            staffFamily = staffFamilyService.findById(familyId);
        }

        ModelAndView mav = new ModelAndView("staff/stafffamily/detail");
        mav.addObject("staffFamily", staffFamily);
        mav.addObject("familyList", StaffFamilyDict.getFamilyList());

        return mav;
    }

}
